﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using NetgameProtocol;

namespace _2DgameServer.MysqlData
{
    class UserData
    {

        ////数据库连接
        //private MySqlConnection mysqlCon;

        //private string connstr = "database=wk6;data source=localhost;user=root;password=123456;pooling=false;charset=utf8;port=3306";

        ////用户数据构造函数
        //public UserData()
        //{
        //    ConnectMysql();
        //}
        //private void ConnectMysql()
        //{
        //    //判断是否成功连接数据库
        //    try
        //    {
        //        mysqlCon = new MySqlConnection(connstr);
        //        mysqlCon.Open();
        //    }
        //    catch(Exception e)
        //    {
        //        Console.WriteLine("连接数据库失败"+e.Message);
        //    }
        //}
        //判断是否注册成功
        public bool Logon(MainPack pack, MySqlConnection sqlConnection)
        {
            string username = pack.Loginpack.Username;
            string password = pack.Loginpack.Password;

            //查询数据，判断用户名是否已经注册
            //string sql = "select * from wk6.userdata where username='@username'";
            //MySqlCommand comd = new MySqlCommand(sql, mysqlCon);
            //MySqlDataReader read = comd.ExecuteReader();
            //if (read.Read())
            //{
            //    //用户名已经被注册
            //    return false;

            //}
            //插入数据 
            //  string sql = "insert into 'wk6'.'userdata'('username', 'password') VALUES('@username', '@password')";
            //MySqlCommand comd = new MySqlCommand();
            try
            {
                string sql = "insert into `game`.`playeraccount`(`username`, `password`) VALUES('" + username + "','" + password + "')";
                MySqlCommand comd = new MySqlCommand(sql, sqlConnection);
                comd = new MySqlCommand(sql, sqlConnection);
                comd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        /// <summary>
        /// 登录数据库操作
        /// </summary>
        public bool Login(MainPack pack, MySqlConnection sqlConnection)
        {
            string username = pack.Loginpack.Username;
            string password = pack.Loginpack.Password;
            try
            {
                string sql = "select * from playeraccount where username = '" + username + "'and password = '" + password + "'";
                MySqlCommand comd = new MySqlCommand(sql, sqlConnection);
                MySqlDataReader read = comd.ExecuteReader();
                bool result = read.HasRows;
                read.Close();
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
    }
}
